package leetcode;

import java.util.function.IntConsumer;

/**
 *  当n=1，打印01
 *  当n=2，打印0102
 *  当n=3，打印010203
 *  ...
 *  当n=i，打印010203....0i
 */
class Code1116_ZeroEvenOdd {
    private int n;
    private final Object lock = new Object();
    private volatile boolean produced = false;
    private Integer num = 1;
    public Code1116_ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        synchronized(lock){
            for(int i = 0 ; i < n ; i++){
                if(produced) lock.wait();
                System.out.print(0);
//                printNumber.accept(i);
                produced = true;
                lock.notifyAll();
            }
        }
    }

    //打印奇数
    public void odd(IntConsumer printNumber) throws InterruptedException {
        // 如果n是奇数，那么一直等待到n。如果n是偶数，那么等待到n-1就可以退出
        int bound = (n & 1) == 0 ? n-1 : n;
        synchronized(lock){
            while(num <= bound){
                //偶数则等待
                while (!produced || num % 2 == 0){
                    lock.wait();
                }
                if(num > bound) break;
                //奇数打印,并告诉去生产0
                System.out.print(num++);
//                printNumber.accept(num++);
                produced = false;
                lock.notifyAll();
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        // 如果n是奇数，那么一直等待到n。如果n是偶数，那么等待到n就可以退出
        int bound = (n & 1) == 0 ? n : n -1;
        synchronized(lock){
            while(num <= bound){
                //奇数则等待
                while (!produced || num % 2 != 0) {
                    lock.wait();
                }
                if(num > bound) break;
                //偶数打印，并告诉produced = false
                System.out.print(num++);
//                printNumber.accept(num++);
                produced = false;
                lock.notifyAll();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i < 30; i++) {
            Code1116_ZeroEvenOdd zeroEvenOdd = new Code1116_ZeroEvenOdd(i);
            new Thread(()->{
                try {
                    zeroEvenOdd.zero(new IntConsumer() {
                        @Override
                        public void accept(int value) {
                            System.out.print(value);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            new Thread(()->{
                try {
                    zeroEvenOdd.even(new IntConsumer() {
                        @Override
                        public void accept(int value) {
                            System.out.print(value);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            new Thread(()->{
                try {
                    zeroEvenOdd.odd(new IntConsumer() {
                        @Override
                        public void accept(int value) {
                            System.out.print(value);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            Thread.sleep(50);
            System.out.println();
        }

    }
}